home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / sossnt.zip / SOSSNT / SRC / NETD.C < prev    next >
C/C++ Source or Header  |  1993-03-03  |  5KB  |  199 lines

  1. /*
  2.  *  netd.c -- 
  3.  *      Net daemon for PC NFS file server.  It sets up and
  4.  *      arbitrates the port mapper daemon, mount daemon and
  5.  *      the NFS server.
  6.  *
  7.  *  Author:
  8.  *      See-Mong Tan, 6/11/88
  9.  *  Modified by:
  10.  *      Rich Braun, 3/29/91
  11.  *
  12.  *  Revision history:
  13.  *  
  14.  * $Log: netd.c_v $
  15.  * Revision 1.5  1991/05/13  17:44:55  richb
  16.  * Add the -b command line option to specify read/write request size.
  17.  *
  18.  * Revision 1.4  1991/04/11  20:39:04  richb
  19.  * Add -t option and use standard parser (getopt).
  20.  *
  21.  */
  22.  
  23. #ifdef RCSID
  24. static char _rcsid_ = "$Id: netd.c_v 1.5 1991/05/13 17:44:55 richb Exp $";
  25. #endif
  26.  
  27. #include "common.h"
  28.  
  29. extern int getopt();
  30. extern int sock_start();
  31. extern void svc_init();
  32. extern long coreleft();
  33.  
  34. bool_t NFS_VERBOSE = FALSE;    /* flag for nfs verbose mode */
  35. bool_t NFS_READONLYFS = FALSE;    /* true if started as a read only filesystem */
  36. bool_t NFS_TRUNCATENAMES = FALSE; /* true if long names should be truncated */
  37. int    nfsrd_size = RD_SIZ;    /* I/O read default size */
  38. int    nfswr_size = WR_SIZ;    /* I/O write default size */
  39.  
  40. main(argc, argv)
  41.     int argc;
  42.     char *argv[];
  43. {
  44. time_t now;
  45.  
  46.     netd_init(argc, argv);        /* initialize netd */
  47.  
  48.     time (&now);
  49.     (void) printf("SOSS v%s %s (compiled %s)\n\nStarting net services at %s",
  50.               VERSION_NUM, VERSION_DATE, __DATE__, ctime (&now));
  51.  
  52.     sock_start();
  53.     svc_init();
  54.  
  55.     /* initialize and register services */
  56.     if (! pmap_init())
  57.         netd_Punt("portmapper");
  58.     DBGPRT0 (nfsdebug, "netd: port mapper created");
  59.  
  60.     if (! mountd_init())
  61.         netd_Punt("mount daemon");
  62.     DBGPRT0 (nfsdebug, "netd: mount daemon created");
  63.  
  64.     if (! nfs_init())
  65.         netd_Punt("nfs server");
  66.     DBGPRT0 (nfsdebug, "netd: nfs server created");
  67.  
  68.     if (! inode_init())
  69.         netd_Punt("inode interface");
  70.  
  71.     if (! dtime_init())
  72.         netd_Punt("dos time interface");
  73.  
  74.     (void) printf("netd: port mapper, mountd and nfs server running\n\n");
  75.     DBGPRT1 (nfsdebug, "Memory available = %ld\n", coreleft());
  76.     (void) svc_run();       /* wait for and service net requests */
  77.     netd_Punt("net daemon returned");
  78.     return 0;
  79. }
  80.  
  81. long
  82. coreleft(void)
  83. {
  84.     MEMORYSTATUS m;
  85.     GlobalMemoryStatus(&m);
  86.     return (long) m.dwAvailPageFile;
  87. }
  88.  
  89.  
  90. /*
  91.  *  void netd_Punt(char *s) --
  92.  *      Prints net daemon error message and exits.
  93.  *      For irrecoverable errors.
  94.  */
  95. void netd_Punt(s)
  96.     char *s;
  97. {
  98.     int i;
  99.  
  100.     (void) fprintf(stderr, "net daemon error: %s\n", s);
  101.     /* Does this really work? */
  102.     for(i = 0; i < 3; i++)
  103.         closesocket(i);
  104.     exit(1);
  105. }
  106.  
  107. int
  108. netd_daemon(void)
  109. {
  110.     SECURITY_ATTRIBUTES saPipe; 
  111.     PROCESS_INFORMATION pi;
  112.     STARTUPINFO si;  /* for CreateProcess call */
  113.     int r;
  114.     char *cmd = "soss";
  115.  
  116.     saPipe.nLength = sizeof(SECURITY_ATTRIBUTES);
  117.     saPipe.lpSecurityDescriptor = NULL;
  118.     saPipe.bInheritHandle = FALSE;
  119.  
  120.     memset(&si, 0, sizeof(si));
  121.     si.cb = sizeof(si);
  122.     r = CreateProcess(NULL,  /* filename */
  123.         cmd,  /* full command line for child */
  124.         NULL,  /* process security descriptor */
  125.         NULL,  /* thread security descriptor */
  126.         FALSE,  /* inherit handles? */
  127.         0,  /* creation flags */
  128.         NULL,  /* inherited environment address */
  129.         NULL,  /* startup dir; NULL = start in current */
  130.         &si,  /* pointer to startup info (input) */
  131.         &pi);  /* pointer to process info (output) */
  132.     if (!r) {
  133.         fprintf(stderr,"CreateProcess() failed!\n");
  134.         return 1;
  135.     }
  136.     CloseHandle(pi.hThread);
  137.     CloseHandle(pi.hProcess);
  138.     return 0;
  139. }
  140.  
  141. /*
  142.  *  void netd_init(int argc, char **argv) --
  143.  *      Initializes the net daemon.  Should be called before any other
  144.  *      routine in the server.
  145.  */
  146. void netd_init(argc, argv)
  147.     int argc;
  148.     char *argv[];
  149. {
  150.         dbg_init ();
  151.     netd_parsecmdln(argc, argv);    /* parse command line */
  152.     signal(SIGINT, netd_break);    /* break handler */
  153. }
  154.  
  155. /*
  156.  *  void netd_break() --
  157.  *      Break handler.  Closes all sockets and exits.
  158.  */
  159. void netd_break(int x)
  160. {
  161.     int i;
  162.  
  163.     (void) fprintf(stderr, "Netd:  break caught... exiting\n");
  164.     for(i = 0; i < 3; i++)
  165.         closesocket(i);
  166.  
  167.     exit(1);
  168. }
  169.  
  170. /*
  171.  *  void netd_parsecmdln(int argc, char **argv) --
  172.  *      Parse command line arguments.
  173.  */
  174. void netd_parsecmdln(argc, argv)
  175.     int argc;
  176.     char *argv[];
  177. {
  178.     int c;
  179.     extern int optind;
  180.     extern char *optarg;
  181.     int err = FALSE;
  182.  
  183.     while ((c = getopt (argc, argv, "b:drtv")) != -1)
  184.       switch (c) {
  185.     case 'b':       if (sscanf (optarg, "%d", &nfsrd_size) == 1)
  186.                  nfswr_size = nfsrd_size;
  187.               else
  188.                err = TRUE;
  189.               break;
  190.     case 'd':    printf("NETD_DAEMON!\n"); netd_daemon(); exit(0);
  191.     case 'r':    NFS_READONLYFS = TRUE;    break;
  192.     case 't':    NFS_TRUNCATENAMES = TRUE; break;
  193.     case 'v':    NFS_VERBOSE = TRUE;      break;
  194.     default:        err = TRUE;
  195.       }
  196.       if (err || optind < argc)
  197.     netd_Punt("Usage: soss [ -b blocksize -rtv ]");
  198. }        
  199.